// 状态
// 左走 右走 跳跃 攻击 静止
const enum State{
    left, right, jump, atttack, idle,
};

const {ccclass, property} = cc._decorator;

@ccclass
export default class Village extends cc.Component {


    @property({displayName: "角色", tooltip: "角色", type: sp.Skeleton})
    player: sp.Skeleton = null;




    @property({displayName: "左走按钮", tooltip: "左走按钮", type: cc.Node})
    left_walk_btn: cc.Node = null;

    @property({displayName: "右走按钮", tooltip: "右走按钮", type: cc.Node})
    right_walk_btn: cc.Node = null;

    @property({displayName: "攻击按钮", tooltip: "攻击按钮", type: cc.Node})
    attack_btn: cc.Node = null;

    @property({displayName: "跳跃按钮", tooltip: "跳跃按钮", type: cc.Node})
    jump_btn: cc.Node = null;




    @property({displayName: "显示生命值的文字", tooltip: "显示生命值的文字", type: cc.Label})
    HP_lb: cc.Label = null;

    @property({displayName: "显示蓝的文字", tooltip: "显示蓝的文字", type: cc.Label})
    MP_lb: cc.Label = null;

    @property({displayName: "显示经验值的文字", tooltip: "显示经验值的文字", type: cc.Label})
    EXP_lb: cc.Label = null;

    @property({displayName: "显示等级的文字", tooltip: "显示等级的文字", type: cc.Label})
    LV_lb: cc.Label = null;




    @property({displayName: "生命值进度条", tooltip: "生命值进度条", type: cc.Sprite})
    HP_sp: cc.Sprite = null;

    @property({displayName: "蓝进度条", tooltip: "蓝进度条", type: cc.Sprite})
    MP_sp: cc.Sprite = null;

    @property({displayName: "经验值进度条", tooltip: "经验值进度条", type: cc.Sprite})
    EXP_sp: cc.Sprite = null;




    @property({displayName: "行走速度", tooltip: "行走速度", type: cc.Float})
    walk_speed: number = 200;

    @property({displayName: "跳跃高度", tooltip: "跳跃高度", type: cc.Float})
    jump_height: number = 200;

    /* @property({displayName: "跳跃后禁用几秒按钮", tooltip: "跳跃后禁用几秒按钮", type: cc.Float})
    jump_time: number = 1.6; */

    @property({displayName: "攻击后禁用几秒按钮", tooltip: "攻击后禁用几秒按钮", type: cc.Float})
    attack_time: number = 0.7;




    is_jump_OK: boolean = true;// 是否可以跳跃
    is_attack_OK: boolean = true;// 是否可以攻击


    state = State.idle;// 状态，默认为静止


    // 攻击的数字，根据这个数字来决定播放哪个攻击动画
    attack_num: number = 1;


    LV: number = 1;// 等级
    full_HP: number = 500;// 生命值
    full_MP: number = 500;// 蓝
    full_EXP: number = 500;// 经验值

    cur_HP: number = 500;// 生命值
    cur_MP: number = 500;// 蓝
    cur_EXP: number = 500;// 经验值



    user_data = JSON.parse(cc.sys.localStorage.getItem("user_data"));




    onLoad () {
        // 给左走按钮绑定事件
        this.left_walk_btn.on(cc.Node.EventType.TOUCH_START, this.left_walk, this);
        this.left_walk_btn.on(cc.Node.EventType.TOUCH_END, this.idle_walk, this);
        this.left_walk_btn.on(cc.Node.EventType.TOUCH_CANCEL, this.idle_walk, this);

        // 给右走按钮绑定事件
        this.right_walk_btn.on(cc.Node.EventType.TOUCH_START, this.right_walk, this);
        this.right_walk_btn.on(cc.Node.EventType.TOUCH_END, this.idle_walk, this);
        this.right_walk_btn.on(cc.Node.EventType.TOUCH_CANCEL, this.idle_walk, this);

        // 给跳跃和攻击按钮绑定事件
        this.attack_btn.on(cc.Node.EventType.TOUCH_START, this.attack, this);
        this.jump_btn.on(cc.Node.EventType.TOUCH_START, this.jump, this);





        // 等级 经验 生命值 蓝

        // 处理好数值
        this.LV = Number(this.user_data.LV);
        this.cur_EXP = this.user_data.EXP;
        this.full_HP = this.LV * 500;
        this.full_MP = this.LV * 250;

        this.cur_HP = this.full_HP;
        this.cur_MP = this.full_MP;
        this.full_EXP = this.LV * 1500;


        // 显示在文字上
        this.HP_lb.string = "HP   " + this.cur_HP + "/" + this.full_HP;
        this.MP_lb.string = "MP   " + this.cur_MP + "/" + this.full_MP;
        this.EXP_lb.string = "EXP   " + this.cur_EXP + "/" + this.full_EXP;
        this.LV_lb.string = "LV:" + this.LV;


        // 进度也显示
        this.HP_sp.fillRange = this.cur_HP / this.full_HP;
        this.MP_sp.fillRange = this.cur_MP / this.full_MP;
        this.EXP_sp.fillRange = this.cur_EXP / this.full_EXP;
    }

    update () {
        // 如果状态为静止
        if (this.state == State.idle) {
            // 打印log
            // cc.log("状态： 静止");

            // 如果动画不为静止
            if (this.player.animation != "idle") {
                // 动画为静止
                this.player.animation = "idle";
            }
            // 角色刚体线性速度X为0
            this.player.getComponent(cc.RigidBody).linearVelocity = cc.v2(0, this.player.getComponent(cc.RigidBody).linearVelocity.y);
        }
        // 如果状态为左走 
        else if (this.state == State.left) {
            // 打印log
            // cc.log("状态： 左走");

            // 如果动画不为走路
            if (this.player.animation != "walk") {
                // 动画为走路
                this.player.animation = "walk";
            }
            // 角色刚体线性速度X为向左的行走速度
            this.player.getComponent(cc.RigidBody).linearVelocity = cc.v2(-this.walk_speed, this.player.getComponent(cc.RigidBody).linearVelocity.y);
            // 人物缩放
            this.player.node.scaleX = -0.6;
        }
        // 如果状态为右走 
        else if (this.state == State.right) {
            // 打印log
            // cc.log("状态： 右走");

            // 如果动画不为走路
            if (this.player.animation != "walk") {
                // 动画为走路
                this.player.animation = "walk";
            }
            // 角色刚体线性速度X为向右的行走速度
            this.player.getComponent(cc.RigidBody).linearVelocity = cc.v2(this.walk_speed, this.player.getComponent(cc.RigidBody).linearVelocity.y);
            // 人物缩放
            this.player.node.scaleX = 0.6;
        } 
        // 如果状态为攻击
        else if (this.state == State.atttack) {
            // 打印log
            // cc.log("状态： 攻击");
        }
        // 如果状态为跳跃
        else if (this.state == State.jump) {
            // 打印log
            // cc.log("状态： 跳跃");
        }


        let num = 0;
        // 如果当前经验值大于等于满的经验值
        if (this.cur_EXP >= this.full_EXP) {
            // 如果当前经验值大于了满的经验值
            if (this.cur_EXP > this.full_EXP) {
                // 获取
                num = this.cur_EXP - this.full_EXP;
            }
            // 升级
            this.upgrade();

            // 增加经验
            this.add_EXP(num);
        }

        
    }

    // 左走函数
    left_walk () {
        this.state = State.left;
    }

    // 右走函数
    right_walk () {
        this.state = State.right;
    }

    // 静止函数
    idle_walk () {
        this.state = State.idle;
    }

    // 跳跃函数
    jump () {
        // 如果可以跳跃
        if (this.is_jump_OK == true) {
            let self = this;
            // 状态为跳跃
            this.state = State.jump;
            // 不可跳跃
            self.is_jump_OK = false;
            // 动画为空
            this.player.animation = null;
            // 清理管道
            this.player.clearTracks();
            // 设置线速度
            this.player.getComponent(cc.RigidBody).linearVelocity = cc.v2(this.player.getComponent(cc.RigidBody).linearVelocity.x, this.jump_height);
            // 添加动画  0号管道 跳跃动画 不循环播放 0秒延迟
            this.player.addAnimation(0, "jump", false, 0);
            // 在动画结束后
            this.player.setCompleteListener(function () {
                // 如果状态为跳跃
                if (self.state == State.jump) {
                    // 状态变为idle
                    self.state = State.idle;
                    // 清理管道
                    self.player.clearTracks();
                }
            });

            /* // 在规定的时间后恢复禁用
            this.scheduleOnce(function () {
                self.is_jump_OK = true;
            }, this.jump_time); */

        }
    }

    // 攻击函数
    attack () {
        // 如果可以攻击
        if (this.is_attack_OK == true) {
            let self = this;
            // 不可攻击
            this.is_attack_OK = false;
            // 状态为攻击
            this.state = State.atttack;
            // 在规定的时间后恢复禁用
            this.scheduleOnce(function () {
                self.is_attack_OK = true;
            }, this.attack_time);



            // 如果攻击数字为1
            if (this.attack_num == 1) {
                // 攻击数字为2
                this.attack_num = 2;
                // 动画为空
                this.player.animation = null;
                // 清空管道
                this.player.clearTracks();
                // 添加动画 0号管道 攻击动画1 不循环 0秒延迟
                this.player.addAnimation(0, "attack1", false, 0);
                // 在动画播放完后
                this.player.setCompleteListener(function () {
                    // 如果状态为攻击
                    if (self.state == State.atttack) {
                        // 状态为idle
                        self.state = State.idle;
                        // 清空管道
                        self.player.clearTracks();
                    }
                });
                // 打印log
                cc.log("攻击1");
                // 以下代码不执行
                return;
            }



            // 如果攻击数字为2
            if (this.attack_num == 2) {
                // 攻击数字为3
                this.attack_num = 3;
                // 动画为空
                this.player.animation = null;
                // 清空管道
                this.player.clearTracks();
                // 添加动画 0号管道 攻击动画2 不循环 0秒延迟
                this.player.addAnimation(0, "attack2", false, 0);
                // 在动画播放完成后
                this.player.setCompleteListener(function () {
                    // 如果状态为攻击
                    if (self.state == State.atttack) {
                        // 状态为idle
                        self.state = State.idle;
                        // 清空管道
                        self.player.clearTracks();
                    }
                });
                // 打印log
                cc.log("攻击2");
                // 以下代码不执行
                return;
            }



            // 如果攻击数字为3
            if (this.attack_num == 3) {
                // 攻击数字为1
                this.attack_num = 1;
                // 动画为空
                this.player.animation = null;
                // 清空管道
                this.player.clearTracks();
                // 添加动画 0号管道 攻击动画3 不循环 0秒延迟
                this.player.addAnimation(0, "attack3", false, 0);
                // 在动画播放完成后
                this.player.setCompleteListener(function () {
                    // 如果状态为攻击
                    if (self.state == State.atttack) {
                        // 状态为idle
                        self.state = State.idle;
                        // 清空管道
                        self.player.clearTracks();
                    }
                });
                // 打印log
                cc.log("攻击3");
                // 以下代码不执行
                return;
            }
        }
    }


    // 人物升级专用函数
    upgrade () {
        // 等级 + 1
        this.LV += 1;
        // 当前经验值为0
        this.cur_EXP = 0;


        // 两个结合在一起
        let user_data = {
            LV: this.LV,
            EXP: 0,
        }

        // 存起来
        cc.sys.localStorage.setItem("user_data", JSON.stringify(user_data));



        // 等级 经验 生命值 蓝

        // 处理好数值
        this.cur_EXP = 0;
        this.full_HP = this.LV * 500;
        this.full_MP = this.LV * 250;

        this.cur_HP = this.full_HP;
        this.cur_MP = this.full_MP;
        this.full_EXP = this.LV * 1500;


        // 显示在文字上
        this.HP_lb.string = "HP   " + this.cur_HP + "/" + this.full_HP;
        this.MP_lb.string = "MP   " + this.cur_MP + "/" + this.full_MP;
        this.EXP_lb.string = "EXP   " + this.cur_EXP + "/" + this.full_EXP;
        this.LV_lb.string = "LV:" + this.LV;


        // 进度也显示
        this.HP_sp.fillRange = this.cur_HP / this.full_HP;
        this.MP_sp.fillRange = this.cur_MP / this.full_MP;
        this.EXP_sp.fillRange = this.cur_EXP / this.full_EXP;

        cc.log("升级了");
    }


    // 增加经验值专用函数   num为加多少
    add_EXP (num: number) {
        this.cur_EXP += num;

        // 两个结合在一起
        let user_data = {
            LV: this.LV,
            EXP: (this.cur_EXP),
        }

        // 存起来
        cc.sys.localStorage.setItem("user_data", JSON.stringify(user_data));


        // 显示在文字上
        this.HP_lb.string = "HP   " + this.cur_HP + "/" + this.full_HP;
        this.MP_lb.string = "MP   " + this.cur_MP + "/" + this.full_MP;
        this.EXP_lb.string = "EXP   " + this.cur_EXP + "/" + this.full_EXP;
        this.LV_lb.string = "LV:" + this.LV;


        // 进度也显示
        this.HP_sp.fillRange = this.cur_HP / this.full_HP;
        this.MP_sp.fillRange = this.cur_MP / this.full_MP;
        this.EXP_sp.fillRange = this.cur_EXP / this.full_EXP;
    } 





}
